package com.dabbo.starship.app.text.client;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.dabbo.starship.interfaces.Block;
import com.dabbo.starship.interfaces.Cell;
import com.dabbo.starship.interfaces.Container;
import com.dabbo.starship.shared.CellLocation;

public class TestClient {
	
	public static void log(String msg) {
		System.out.println(new Date() + " :: " + msg);
	}

	public static void main(String[] args) {

		Registry registry;
		Container cp = null;
		
		try {
			registry = LocateRegistry.getRegistry("localhost", 3232);
			cp = (Container)(registry.lookup("shp"));
			
			log("Current Power : " + cp.getPowerOutput());
			
			Set<Entry<Integer, Block>> set = cp.getAllBlocks().entrySet(); 
			Iterator<Entry<Integer, Block>> i = set.iterator(); 
			while(i.hasNext()) { 
				Map.Entry<Integer, Block> ent = (Map.Entry<Integer, Block>) i.next();
				Block blk = (Block) ent.getValue();
				blk.setLighting(true);
			} 

			if (cp.getPowerOutput() < 0) {
				log("Power failure");
				cp.powerFailure();
				log("After Power Failure : " + cp.getPowerOutput());
			}

			if (cp.getPowerOutput() == 0) {
				log("Searching for Reactor Rooms...");
				i = set.iterator(); 
				while(i.hasNext()) { 
					Map.Entry<Integer, Block> ent = (Map.Entry<Integer, Block>) i.next();
					Block blk = (Block) ent.getValue();
					log("    Found room : " + blk.getDescription());
					if (blk.getBlockType() == Block.BLOCK_REACTOR) {
						log("        Powering up reactor...");
						blk.activateCell(new CellLocation(3, 3));
						blk.activateCell(new CellLocation(3, 5));
						blk.activateCell(new CellLocation(5, 3));
						blk.activateCell(new CellLocation(5, 5));
						log("        Power sequence completed.");
					}
				} 
				
			}
			
			checkCorridors(cp, 1, 1);			
			
		} catch (RemoteException ex) {
			ex.printStackTrace();
		} catch (NotBoundException ex) {
			ex.printStackTrace();
		}

		log("Saving object to file...");
		try {
			FileOutputStream fout = new FileOutputStream("object.out");
			ObjectOutputStream oos = new ObjectOutputStream(fout);   
			oos.writeObject(cp);
			oos.close();
		} catch (Exception ex) {
			log("Failed.");
		}

		log("Done.");
		System.exit(0);

	}

	public static void checkCorridors(Container cp, int cy, int cx) throws RemoteException {
		Block blk = cp.getBlock(cp.getEntryBlock());

		log("(" + cy + ", " + cx + ") " + blk.getDescription() + " room layout:");
		
		for (int y = 0; y < 9; y++) {
			for (int x = 0; x < 9; x++) {
				switch(blk.getCell(new CellLocation(y, x)).getCellType()) {
				case Cell.CELL_DEFAULT:
					System.out.print("#");
					break;
				case Cell.CELL_DOOR:
					System.out.print("D");
					break;
				case Cell.CELL_FLOOR:
					System.out.print(" ");
					break;
				case Cell.CELL_POWER:
					System.out.print("P");
					break;
				case Cell.CELL_ENGINE:
					System.out.print("E");
					break;
				case Cell.CELL_ENGINE_CORE:
					System.out.print("e");
					break;
				case Cell.CELL_BED:
					System.out.print("B");
					break;
				case Cell.CELL_CONSOLE:
					System.out.print("C");
					break;
				case Cell.CELL_MEDBED:
					System.out.print("M");
					break;
				default:
					System.out.println("?");
				}
			}
			System.out.print("\n");
		}
		
	}
	
}
